package com.example.db;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.example.R;

/**
 * Created with IntelliJ IDEA.
 * User: wlai
 * Date: 12/11/12
 * Time: 7:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class AddCategoryActivity extends Activity {
    private Button addButton;
    private Button cancelButton;
    private EditText categoryName;

    private ListView listView;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_category);

        addButton = (Button) findViewById(R.id.addButton);
        cancelButton = (Button) findViewById(R.id.cancelButton);
        categoryName = (EditText) findViewById(R.id.categoryName);
        listView = (ListView) findViewById(R.id.listView);
        final DbHelper dbHelper = new DbHelper(AddCategoryActivity.this);
        context = this.getApplicationContext();
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                long id = dbHelper.addCategory(categoryName.getText().toString());
                Toast.makeText(AddCategoryActivity.this, id + "", Toast.LENGTH_LONG).show();
            }
        });
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AddCategoryActivity.this.finish();
            }
        });


        Cursor cursor = dbHelper.listCategory();
        listView.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_1, cursor, new String[]{"name"},
                new int[]{android.R.id.text1}));
    }
}
